package de.fhaugsburg.games.boardgames.connect4.strategy;

import java.util.TreeSet;

import de.fhaugsburg.games.boardgames.Board;
import de.fhaugsburg.games.boardgames.Piece;
import de.fhaugsburg.games.boardgames.connect4.Connect4Board;
import de.fhaugsburg.games.boardgames.connect4.Connect4Engine;
import de.fhaugsburg.games.boardgames.connect4.IRandomRobot;
import de.fhaugsburg.games.boardgames.connect4.Connect4Engine.Connect4Directions;
import de.fhaugsburg.games.boardgames.exceptions.GameException;

public class RandomRobot implements IRandomRobot {
	
	private int proposal;
	private Connect4Engine engine;
	private TreeSet<Connect4Directions> directionColl;


	
	public RandomRobot(Connect4Engine engine) {
		this.engine = engine;
	}
	

	@Override
	public int columnProposalSimple(Piece piece, Board board) throws GameException {
				
		proposal = (int) (Math.random() * board.getWidth());
		
		while(board.isEmpty(0, proposal) == false){ //if there is in the first row a piece, choose another proposal
			proposal = (int) (Math.random() * board.getWidth());
		}
				
		return proposal;
	}

	@Override
	public int columnProposalIntelligent(Piece piece, Board board) throws GameException {
		int equalPieces = 0;
		directionColl = engine.getdirectionColl();
		for (Connect4Directions element : directionColl) {
			if(element.getEqualPieces() > equalPieces){
				Connect4Directions dir = element;
			equalPieces = element.getEqualPieces();
				if(dir.equals(Connect4Directions.SOUTH)) //if the human player has some pieces horizontal sequenced
					proposal = engine.getCurrentColumn();
				if(dir.equals(Connect4Directions.EAST) || dir.equals(Connect4Directions.WEST))
					proposal += element.getColumnDirection();							
			}
		}
		if(equalPieces > 0){
			return proposal;
		}else{
			return proposal = columnProposalSimple(piece, board);
		}
		
		
		
		
	}
	
	



}
